package service

import (
	"encoding/json"
	"fmt"
	"rongyy/src/helper"
	"rongyy/src/middlewares"
	"rongyy/src/models"
)

type XyjyService struct {
	d *models.YktModel
}

// 签约
func (ds *XyjyService) Agreement(req *models.XyjyReqModel) (*models.XyjyAgreementRespModel, error) {
	pid := req.PatientId
	card, has, err := (&models.CardModel{}).FindByPatientId(pid)
	if !has {
		return nil, fmt.Errorf("请先建卡")
	}
	req.IdNo = card.IdNo
	req.Nme = card.RealName
	fmt.Println("req===", req)
	data, err := helper.PostJson("http://47.109.25.220:8011/xyjy/agreement", req)
	if err != nil {
		middlewares.Logger.Println("Unmarshal resp3013 failed ", err)
		return nil, err
	}
	fmt.Println(string(data))
	d := &models.XyjyAgreementRespModel{}
	err = json.Unmarshal(data, d)
	if err != nil {
		middlewares.Logger.Println("Unmarshal Resp32004 failed ", err)
		return nil, err
	}
	middlewares.Logger.Println("Unmarshal resp3013 failed ", d)
	return d, nil
}

// 退款
func (ds *XyjyService) Check(req *models.XyjyReqModel) (*models.XyjyCheckRespModel, error) {
	data, err := helper.PostJson("http://47.109.25.220:8011/xyjy/check", req)
	if err != nil {
		middlewares.Logger.Println("Unmarshal resp3013 failed ", err)
		return nil, err
	}
	fmt.Println(string(data))
	d := &models.XyjyCheckRespModel{}
	err = json.Unmarshal(data, d)
	if err != nil {
		middlewares.Logger.Println("Unmarshal Resp32004 failed ", err)
		return nil, err
	}
	middlewares.Logger.Println("Unmarshal resp3013 failed ", data)
	return d, nil
}

// 付款
func (ds *XyjyService) Pay(req *models.XyjyReqModel) (*models.XyjyPayRespModel, error) {
	pid := req.PatientId
	card, has, err := (&models.CardModel{}).FindByPatientId(pid)
	if !has {
		return nil, fmt.Errorf("请先建卡")
	}
	fmt.Println(card)
	req.IdNo = card.IdNo
	req.Nme = card.RealName
	//req.IdNo = "310105200012184123"
	//req.Nme = "赵站昊"
	// 检查额度

	data, err := helper.PostJson("http://47.109.25.220:8011/xyjy/pay", req)
	if err != nil {
		middlewares.Logger.Println("Unmarshal resp3013 failed ", err)
		return nil, err
	}
	d := &models.XyjyPayRespModel{}
	err = json.Unmarshal(data, d)
	if err != nil {
		middlewares.Logger.Println("Unmarshal Resp32004 failed ", err)
		return nil, err
	}
	middlewares.Logger.Println("Unmarshal resp3013 failed ", data)
	return d, nil
}

// 退款
func (ds *XyjyService) Refund(req *models.XyjyRefundReqModel) (*models.XyjyRefundRespModel, error) {
	data, err := helper.PostJson("http://47.109.25.220:8011/xyjy/refund", req)
	if err != nil {
		middlewares.Logger.Println("Unmarshal resp3013 failed ", err)
		return nil, err
	}
	fmt.Println(string(data))
	d := &models.XyjyRefundRespModel{}
	err = json.Unmarshal(data, d)
	if err != nil {
		middlewares.Logger.Println("Unmarshal Resp32004 failed ", err)
		return nil, err
	}
	middlewares.Logger.Println("Unmarshal resp3013 failed ", data)
	return d, nil
}
